const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  ws.on('message', (message) => {
    // 模拟AI回复（实际应调用AI接口）
    const aiResponse = `AI回复：${message}`;
    
    // 广播消息给所有客户端
    wss.clients.forEach(client => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(JSON.stringify({
          type: 'message',
          content: aiResponse,
          timestamp: new Date().getTime()
        }));
      }
    });
  });
});

console.log('WebSocket服务器运行在 ws://localhost:8080');
